/**
 * Author zdc
 * 公共函数
 */
function getUrlParameter(property,url) {
    var LocString = url||String(window.location.href);
    var rs = new RegExp("(^|)" + property + "=([^\&]*)(\&|$)", "gi")
        .exec(LocString), tmp;
   
    if (tmp = rs) {
    	return tmp[2].replace(/\s/g,encodeURI(" "));
    }
    return "";
};


function showLoading(){
//	 $('body').showLoading();
	var message = Global.language == "zh-cn"?"正在加载...":"Loading...";
	var bc ='<div class="modal-backdrop  in"></div>';
	var load = '<div class="blockui modal" style="display: block;"><div class="modal-dialog"><img class="ball" src="images/ball.png" /><div class="msg">'+message+'</div></div></div>';
	$('body').append(bc);
	$('body').append(load);
}
function hideLoading(){
//	$('body').hideLoading();
	$('body > .modal-backdrop').remove();
	$('body > .blockui').remove();
}
function showTxtLoading(){
	$('body').showLoading();
}
function hideTxtLoading(){
	$('body').hideLoading();
}
/**
 * 显示自定义的提示消息
 * @param msg 提示消息
 * @param bError 是否为错误提示
 */
function showMessage(msg, time, dom, bError) {
	
	require(["widgets/tooltip/Tooltip"],function(Tooltip){
		var speed = 8;//8字符/秒
	    time = Math.ceil(msg.length / speed) * 1000;
	    bError = (bError != undefined) ? bError : true;
	    new Tooltip({"contentStr":msg, time:time, relativeParent:dojo.body(), bError:bError});
	});
    
}
function confirmInfo(info,callback,title){
	require(["widgets/formWindow/confirmInfoWidget/ConfirmWidget"],function(ConfirmWidget){
		var win=new ConfirmWidget({
			text:info,
			title:title||"",callback:callback
		});
		win.startup();
		win.placeAt(dojo.body());
	});
}

/**
 * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
 * 即谷歌、高德 转 百度
 * @param lng
 * @param lat
 * @returns {*[]}
 */
function gaodeToBaidu(lng, lat) {
	var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
    var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
    var bd_lng = z * Math.cos(theta) + 0.0065;
    var bd_lat = z * Math.sin(theta) + 0.006;
    return [bd_lng, bd_lat];
}
function baiduToGaode(bd_lon, bd_lat) {
	var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    var x = bd_lon - 0.0065;
    var y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    var gg_lng = z * Math.cos(theta);
    var gg_lat = z * Math.sin(theta);
    return [gg_lng, gg_lat]
}

//百分比四舍五入后归一
function evenRound(arr) {
	var decimal = -~arr.map(function( a ){ return a % 1; })
    .reduce(function( a,b ){ return a + b; }); // Ceil of total sum of decimals
	for ( var i = 0; i < decimal; ++i ) {
		arr[ i ] = ++arr[ i ]; // compensate error by adding 1 the the first n items
  	}
	return arr.map(function( a ){ return ~~a; }); // floor all other numbers
}
//因iphone单选框风格异常做的调整
function resetPhoneRadioBtn(node){
	if(Global.device == 'phone'){
		var domsY,domsN ;
		if(node){
			domsY = dojo.query(".en-yes",node);
			domsN = dojo.query(".en-no",node);
		}else{
			domsY = dojo.query(".en-yes");
			domsN = dojo.query(".en-no");
		}
		if ((domsY&&domsN)&&domsY.length==domsN.length){
			for (var i = 0; i < domsY.length; i++){
				var tmpYtxt = $(domsY[i]).text();
				var tmpNtxt = $(domsN[i]).text();
				domsY[i].innerHTML = dojo.query("input",domsY[i])[0].outerHTML + tmpNtxt;
				domsN[i].innerHTML = dojo.query("input",domsN[i])[0].outerHTML + tmpYtxt;
			}
		}
	}
}
function   formatDate(date)   {     
	var   year=date.getYear()+1900;     
	var   month=date.getMonth()+1;     
	var   day=date.getDate();     
	var   hour=date.getHours();     
	var   minute=date.getMinutes();     
	var   second=date.getSeconds();     
	return   year+"年"+month+"月"+day+"日 ";//+hour+":"+(minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second);     
}
function   getDateStr()   {   
	var date = new Date();
	var   year=date.getYear()+1900;     
	var   month=date.getMonth()+1;     
	var   day=date.getDate();     
	var   hour=date.getHours();     
	var   minute=date.getMinutes();     
	var   second=date.getSeconds();     
	return   year+""+(month<10?"0"+month:month)+""+(day<10?"0"+day:day)+""+(hour<10?"0"+hour:hour)+""+(minute<10?"0"+minute:minute)+""+(second<10?"0"+second:second);     
}
function   reduceformatDate(days,splitstr)   {  
	var val = days*24*60*60*1000;
	var targetdate = new Date(new Date().getTime()-val);
	var   year=targetdate.getYear()+1900;     
	var   month=targetdate.getMonth()+1;     
	var   day=targetdate.getDate();     
//	return   targetdate.getTime();   
	return   year+splitstr+(month>9?month:"0"+month)+splitstr+(day>9?day:"0"+day);
} 
var dateFormatToString = function(value, key, data){
	if(value==null||value.trim()==''){
		return "";
	}
	 return formatDate(new Date(value));
};
function listSortBy(arr, field, order){ 
    var refer = [], result=[], order = order=='asc'?'asc':'desc', index; 
    for(var i=0; i<arr.length; i++){ 
    	var obj = {val:arr[i][field],idx:i};
        refer.push(obj);
        
    } 
    //refer.sort(); 
    refer.sort(function(a,b){
    	return a.val-b.val});
    if(order=='desc') refer.reverse(); 
    for(i=0;i<refer.length;i++){ 
        index = refer[i].idx; 
        result[i] = arr[index]; 
    } 
    return result; 
}